﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RedHouse.Data.Service;
using RedHouse.Data.Common;

namespace RedHouse.Data.Base
{
    public class SlideShowService : ISlideShowService
    {
          #region Declaration
        private NewRedHouseEntitiesDataContext _dataContext;
        #endregion

        private static ISlideShowService _slideShowService;
        public static ISlideShowService Instance
        {
            get
            {
                if (_slideShowService == null)
                {
                    _slideShowService = new SlideShowService();
                }
                return _slideShowService;
            }
        }

        public SlideShowService()
        {
            _dataContext = new NewRedHouseEntitiesDataContext();
        }

        #region ISlideShowService Members

        public SlideShow GetSlideShowById(int id)
        {
            return _dataContext.SlideShows.Where(s => s.SlideShowId == id).SingleOrDefault();
        }

        public List<SlideShow> GetAllSlideShows()
        {
            return _dataContext.SlideShows.ToList();
        }

        public List<SlideShow> GetAllDisplayedSlideShows()
        {
             return _dataContext.SlideShows.Where(s=>s.IsDisplayed.Value).OrderByDescending(s=>s.SlideShowId).ToList();
        }

        public Common.OperationResult SaveSlideShow(SlideShow slideShow)
        {
            OperationResult result = new OperationResult()
            {
                Message = string.Empty,
                Result = true,
                ReturnObject = slideShow
            };

            try
            {
                _dataContext.SlideShows.InsertOnSubmit(slideShow);
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Result = false;
            }

            return result;
        }

        public Common.OperationResult UpdateSlideShow(SlideShow slideShow)
        {
            OperationResult result = new OperationResult()
            {
                Message = string.Empty,
                Result = true,
                ReturnObject = slideShow
            };

            try
            {
                SlideShow updateItem = _dataContext.SlideShows.Where(s => s.SlideShowId == slideShow.SlideShowId).Single();
                if (updateItem == null)
                {
                    result.Message = "Cannot find the update item";
                    result.Result = false;
                    return result;

                }
                updateItem.Tile = slideShow.Tile;
                updateItem.ThumbImageUrl = slideShow.ThumbImageUrl;
                updateItem.FullSizeImageUrl = slideShow.FullSizeImageUrl;
                updateItem.Description = slideShow.Description;
                updateItem.IsDisplayed = slideShow.IsDisplayed;
                updateItem.LanguageId = slideShow.LanguageId;
                updateItem.LocationId = slideShow.LocationId;
                updateItem.ReferenceUrl = slideShow.ReferenceUrl;
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Result = false;
            }

            return result;
        }

        public Common.OperationResult DeleteSlideShow(int id)
        {
            OperationResult result = new OperationResult()
            {
                Message = string.Empty,
                Result = true,
                ReturnObject = null
            };

            try
            {
                SlideShow deleteItem = _dataContext.SlideShows.Where(s => s.SlideShowId == id).Single();
                if (deleteItem == null)
                {
                    result.Message = "Cannot find the delete item";
                    result.Result = false;
                    return result;

                }

                _dataContext.SlideShows.DeleteOnSubmit(deleteItem);                
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Result = false;
            }

            return result;
        }

        #endregion
    }
}
